﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using WCLOnlineExam.Models;

namespace WCLOnlineExam.Controllers
{
    public class ExamController : EntityController<Exam>
    {
        public ExamController(IEntityContextBuilder builder) : base(builder) { }

        //
        // GET: /Exam/
        public async override Task<ActionResult> Index(int page = 1, int size = 20, string parentpath = null, Guid? parentid = null, bool search = false)
        {
            ViewResult result =(ViewResult)await base.Index(page, size, parentpath, parentid, search);
            EntityViewModel<Exam> model = (EntityViewModel<Exam>)result.Model;
            MvcViewButton button = new MvcViewButton();
            button.StaticLink = "ExamDatil";

            //权限是学生时button显示为“参加考试”，其他情况显示为“试题预览”
            if (((User)Session["user"]).Group.Power.ToString().Equals("Student"))
            {
                button.Name = "参加考试";
            }
            else
            {
                button.Name = "试题预览";
            }
            model.Buttons = new EntityViewButton[] { button };
            return result;
        }
        public ActionResult ExamDatil(Guid id)
        {
            IEntityQueryable<Question> QuestionContext = EntityBuilder.GetContext<Question>();

            IEntityQueryable<Exam> context = EntityBuilder.GetContext<Exam>();
            ViewBag.Exam  = context.Query().Where(c => c.Index == id).First();
            ViewBag.Questions = (ViewBag.Exam).Question;
            ViewBag.ExamId = id.ToString();
            return View();
        }

        public ActionResult TakeExam(Guid id)
        {
            IEntityQueryable<Question> QuestionContext = EntityBuilder.GetContext<Question>();

            IEntityQueryable<Exam> context = EntityBuilder.GetContext<Exam>();
            ViewBag.Exam = context.Query().Where(c => c.Index == id).First();
            ViewBag.Questions = (ViewBag.Exam).Question;
            ViewBag.ExamId = id.ToString();
            return View();
        }

        public ActionResult ExamList()
        {
            return View();
        }
        
	}
}